<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="文章标题">
        <el-input v-model="form.title"/>
      </el-form-item>
      <el-form-item label="关键字">
        <el-tag
          v-for="tag in form.tags"
          :key="tag"
          :disable-transitions="false"
          closable
          @close="handleClose(tag)">
          {{ tag }}
        </el-tag>
        <el-input
          v-if="inputVisible"
          ref="saveTagInput"
          v-model="inputValue"
          class="input-new-tag"
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"/>
        <el-button v-else class="button-new-tag" size="small" @click="showInput">新增</el-button>
      </el-form-item>
      <el-form-item label="摘要">
        <el-input v-model="form.summary"/>
      </el-form-item>
      <el-form-item label="所属类别">
        <el-cascader
          :props="cascaderProp"
          :options="studyTypeList"
          v-model="form.belongType"
          change-on-select/>
      </el-form-item>
      <el-form-item label="文章内容">
        <tinymce :height="300" v-model="form.content"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即创建</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
    <div/>
    <div class="editor-content" v-html="form.content"/>
  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import { selectTree } from '@/api/JStudy'

export default {
  name: 'TinymceDemo',
  components: { Tinymce },
  data() {
    return {
      cascaderProp: {
        value: 'code',
        label: 'text'
      },
      content: ``,
      inputVisible: false,
      inputValue: '',
      form: {
        title: null,
        tags: [],
        summary: '',
        content: '',
        belongType: null
      },
      studyTypeList: []
    }
  },
  mounted() {
    selectTree().then(res => {
      this.studyTypeList = res.data.children
    }).catch(e => {
      console.log(e)
    })
  },
  methods: {
    onSubmit() {},
    handleClose(tag) {
      this.form.tags.splice(this.form.tags.indexOf(tag), 1)
    },
    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    handleInputConfirm() {
      const inputValue = this.inputValue
      if (inputValue) {
        this.form.tags.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    }
  }
}
</script>

<style scoped>
.editor-content{
  margin-top: 20px;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>

